package br.com.aquinoevasconcelos.servlets.orcamento;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import br.com.aquinoevasconcelos.fachada.Fachada;
import br.com.aquinoevasconcelos.modelos.Orcamento;

@WebServlet("/InserirOrcamento")
public class InserirOrcamento extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		Fachada fachada = Fachada.getInstancia();
		Orcamento orcamento = new Orcamento();

		orcamento.setQtdFuncRegistrado(0);
		orcamento.setNomeCompleto(request.getParameter("nomeCompleto"));
		orcamento.setEmail(request.getParameter("emailPessoal"));
		orcamento.setAreaInteresse(request.getParameter("areaInteresse"));
		orcamento.setAtividades(request.getParameter("atividades"));
		orcamento.setConstituicao(request.getParameter("constituicao"));
		orcamento.setObservacoes(request.getParameter("observacao"));
		orcamento.setTributacao(request.getParameter("tributacao"));
	    orcamento.setFaturamentoMensal(request.getParameter("faturamentoMes"));
	    
	    
		String qtdNfServicos = request.getParameter("quantidadeNFServicoMes");
		String qtdNFVendas = request.getParameter("quantidadeNFVendaMes");
		String qtdFuncRegistrado = request.getParameter("qtdFuncionariosRegistrados");
		String qtdEstagiario = request.getParameter("qtdFuncionariosEstagiarios");

	

		if (qtdNfServicos.trim().isEmpty()) {
		
			orcamento.setQtdNFporMesServicos(0);
		
		} 
		
		else if (!qtdNfServicos.trim().isEmpty()) {
			
			orcamento.setQtdNFporMesServicos(Integer.parseInt(qtdNfServicos));
		
		}

		if (qtdNFVendas.trim().isEmpty()) {
		
			orcamento.setQtdNFporMesVendas(0);
		
		}
		
		else if (!qtdNFVendas.trim().isEmpty()) {
		
			orcamento.setQtdNFporMesVendas(Integer.parseInt(qtdNFVendas));
		
		}

		if (qtdEstagiario.trim().isEmpty()) {
		
			orcamento.setQtdEstagiario(0);
		
		}
		
		else if (!qtdEstagiario.trim().isEmpty()) {
		
			orcamento.setQtdEstagiario(Integer.parseInt(qtdEstagiario));
		
		}

		if (qtdFuncRegistrado.trim().isEmpty()) {
			
			orcamento.setQtdFuncRegistrado(0);
		
		}
		
		else if (!qtdFuncRegistrado.trim().isEmpty()) {
			
			orcamento.setQtdFuncRegistrado(Integer.parseInt(qtdFuncRegistrado));
		
		}

		try {

			fachada.inserirOrcamento(orcamento);
			response.sendRedirect("orcamento/orcamento.jsp?msg=sucesso");

		} 
		
		catch (Exception e) {
			
			e.printStackTrace();

			RequestDispatcher rd = request.getRequestDispatcher("../erro.jsp");
			rd.forward(request, response);
		
		}
	
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		service(request, response);

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		service(request, response);

	}

}